/**
 * 虚拟数据脚本-真是
 */
dev.pick = function () {

    var checkSql = 'SELECT DISTINCT dev.id as devId,ici.name as proName, ici.type as type,ici.unit as unit from iot_coll_items ici\n' +
        '        LEFT JOIN iot_dev_type idt on idt.id = ici.dev_type_id\n' +
        '        LEFT JOIN iot_dev dev on dev.dev_type_id = idt.id\n' +
        '        WHERE 1 = 1\n' +
        '        and dev.type = 0';

    var checkResult = iot.makeAndGetCache('PROS_LIST_CACHE', null)
    if (checkResult == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        checkResult = iot.db().queryList(checkSql);
        if (checkResult.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_CACHE', checkResult)
        }
    }

    var sums = iot.list()
    var reals = iot.list()
    var tps = iot.list()
    var ls = iot.list()
    var togglers = iot.list()

    for (var i = 0; i < checkResult.size(); i++) {
        var item = checkResult.get(i)
        var proType = item.type

        if (proType == '累积量') {
            sums.add(item)
        }

        if (proType == '瞬时量') {
            reals.add(item)
        }

        if (proType == '温度' || proType == '压力') {
            tps.add(item)
        }

        if (proType == '露点') {
            ls.add(item)
        }

        if (proType == '开关量') {
            togglers.add(item)
        }
    }

    // 分别处理数据
    cumUp(sums);
    realUp(reals);
    tpUp(tps);
    ldUp(ls);
    togglerUp(togglers);
}

function cumUp (lists) {

    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var getOldValueSql = 'SELECT dvalue, uptime from iot_dev_cur_data_win cur where cur.dev_id = ' + item.devId + ' and data_type = "' + item.proName + '"';
        var oldValueMap = iot.db().queryOne(getOldValueSql);
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 5)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:''

        if (oldValueMap.dvalue == null && oldValueMap.uptime == null) {
            // 插入实时表 （插入 秒粒度的时间）
            var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
            iot.db().updateAsync(replaceCurSql);
            // 插入win表 插入 小时粒度的时间
            var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) value (' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+',"' + item.proName + '",' + dvalue + ')';
            iot.db().updateAsync(insertIntoWinSql);
            continue
        }

        if (oldValueMap.dvalue != null) {
            dvalue = parseInt(oldValueMap.dvalue) + parseInt(dvalue)
        }

        var ifReplace = false;
        if (oldValueMap.uptime != null) {
            var gapMinutes = parseInt(new Date() - new Date(oldValueMap.uptime)) / 1000
            // 20秒插入一条
            if (gapMinutes > 20) {
                ifReplace = true
            }
        }

        if (ifReplace) {
            // 插入实时表
            var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
            iot.db().updateAsync(replaceCurSql);
            // 插入win表
            var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) value (' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
            iot.db().updateAsync(insertIntoWinSql);
        }
    }
}

function realUp (lists) {
    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 145 + 88)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:''
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())'
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
}

function tpUp (lists) {
    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 15 + 15)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:''
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ','
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
}

function ldUp (lists) {
    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i);
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 10 -5);
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH');
        var unit = item.unit?item.unit:'';
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
}

function togglerUp (lists) {
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var getOldValueSql = 'SELECT dvalue, uptime from iot_dev_cur_data_win cur where cur.dev_id = ' + item.devId + ' and data_type = "' + item.proName + '"';
        var oldValueMap = iot.db().queryOne(getOldValueSql);

        var dvalue1 = parseInt(Math.random() * 33)%2;
        var dvalue2 = parseInt(Math.random() * 33)%2;
        var dvalue = dvalue1 + dvalue2
        var unit = ''

        if (oldValueMap.uptime != null) {
            var gapMinutes = parseInt(new Date() - new Date(oldValueMap.uptime)) / 1000 / 60
            // 20分钟插入一条
            if (gapMinutes < 300) {
                dvalue = oldValueMap['dvalue']
            }
            // 插入实时表
            var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
            iot.db().updateAsync(replaceCurSql);
        } else {
            // 插入实时表
            var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
            iot.db().updateAsync(replaceCurSql);
        }
    }
}
